package com.cj.exceptionhandler.redisstart.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

/**
 * 用于捕捉Exception异常以及其子类。 捕捉到之后，把异常信息、发出异常的地址放进ModelAndView里，然后跳转到 error.html页面
 *
 * @author cuijiao
 *
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(value = Exception.class)
    public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
        ModelAndView mav = new ModelAndView();
        if (e instanceof RuntimeException) {
            mav.addObject("errorMsg", e.getMessage());
        } else {
            mav.addObject("errorMsg", "系统异常！");
        }
        mav.addObject("url", req.getRequestURL());
        mav.setViewName("error");
        return mav;
    }
}
